package com.tuju.jetpackfirstdemo.ui.rv_demo2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSnapHelper
import com.tuju.jetpackfirstdemo.R
import com.tuju.jetpackfirstdemo.adapter.ScheduleAdapter
import com.tuju.jetpackfirstdemo.adapter.ScheduleDateAdapter
import com.tuju.jetpackfirstdemo.data.ScheduleBean
import com.tuju.jetpackfirstdemo.utils.showToast
import kotlinx.android.synthetic.main.activity_schedule_demo.*
import java.util.*

class ScheduleDemoActivity : AppCompatActivity(), ScheduleDateAdapter.ClickCallBack {

    private lateinit var scheduleAdapter: ScheduleAdapter
    private lateinit var dateAdapter: ScheduleDateAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_schedule_demo)

        initRecyclerView()
        getDateData()
        getScheduleData("9")
    }

    private fun initRecyclerView() {
        //计划表
        scheduleAdapter = ScheduleAdapter()
        rv_schedule.layoutManager = LinearLayoutManager(this@ScheduleDemoActivity, LinearLayoutManager.HORIZONTAL, false)
        rv_schedule.adapter = scheduleAdapter
        LinearSnapHelper().attachToRecyclerView(rv_schedule)

        //日期
        dateAdapter = ScheduleDateAdapter(this@ScheduleDemoActivity)
        rv_date.layoutManager = LinearLayoutManager(this@ScheduleDemoActivity, LinearLayoutManager.HORIZONTAL, false)
        rv_date.adapter = dateAdapter
        LinearSnapHelper().attachToRecyclerView(rv_date)
    }

    /**
     * 日期点击回调
     */
    override fun onDateItemClick(position: Int, date: String) {
        showToast("当前日期：$date")
        getScheduleData(date)
        dateAdapter.selectPosition = position
        dateAdapter.notifyDataSetChanged()
    }

    //计划时间假数据
    private fun getScheduleData(date: String) {
        val randState = arrayListOf<String>("空", "约", "锁")
        val randName = arrayListOf<String>("二蛋", "白泽", "臭猪", "大宝", "老王", "丁姐", "狗李森", "蔡徐坤", "钢弹", "大喵", "二喵", "二狗")
        val schedules = ArrayList<ScheduleBean>()
        for (str in randName) {
            schedules.add(
                    ScheduleBean(date, str, randState[getRandomNum(0, randState.size - 1)]
                            , randState[getRandomNum(0, randState.size - 1)]
                            , randState[getRandomNum(0, randState.size - 1)]
                            , randState[getRandomNum(0, randState.size - 1)]
                            , randState[getRandomNum(0, randState.size - 1)]
                            , randState[getRandomNum(0, randState.size - 1)]
                            , randState[getRandomNum(0, randState.size - 1)]
                            , randState[getRandomNum(0, randState.size - 1)]
                            , randState[getRandomNum(0, randState.size - 1)]
                            , randState[getRandomNum(0, randState.size - 1)]
                            , randState[getRandomNum(0, randState.size - 1)]
                            , randState[getRandomNum(0, randState.size - 1)]
                    ))
        }
        scheduleAdapter.clear()
//        scheduleAdapter.notifyItemRangeRemoved(0, scheduleAdapter.itemCount)
        scheduleAdapter.addAll(schedules)
//        scheduleAdapter.notifyItemRangeInserted(0, schedules.size)
        scheduleAdapter.notifyDataSetChanged()
//        rv_schedule.smoothScrollToPosition(0) //滚动到第一条
    }

    //获取日期假数据
    private fun getDateData() {
        val date = arrayListOf<String>("9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21")
        dateAdapter.addAll(date)
        dateAdapter.notifyItemRangeInserted(0, date.size)
    }

    //获取范围内随机数
    private fun getRandomNum(min: Int, max: Int): Int {
        val random = Random()
        return random.nextInt((max - min) + 1) + min
    }

}
